package com.ui;

import com.service.StudentService;
import com.model.Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class StudentManagementUI extends JFrame {
    private StudentService studentService;
    private JTable table;
    private DefaultTableModel tableModel;
    private JTextField searchField;
    private String currentClass;

    public StudentManagementUI(StudentService studentService, String currentClass, List<Student> students) {
        this.studentService = studentService;
        this.currentClass = currentClass;
        setTitle("学生管理");
        setSize(600, 400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new BorderLayout());

        // 创建输入框和按钮
        JPanel inputPanel = new JPanel(new FlowLayout());
        searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        JButton deleteButton = new JButton("删除");
        JButton backButton = new JButton("返回");

        inputPanel.add(searchField);
        inputPanel.add(searchButton);
        inputPanel.add(deleteButton);
        inputPanel.add(backButton);

        panel.add(inputPanel, BorderLayout.NORTH);

        // 创建表格模型和表格
        tableModel = new DefaultTableModel(new Object[]{"学号", "姓名"}, 0);
        table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);

        panel.add(scrollPane, BorderLayout.CENTER);

        add(panel);

        // 初始化表格数据
        loadStudentData(students);

        // 查询按钮事件
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                searchStudents();
            }
        });

        // 删除按钮事件
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteSelectedStudent();
            }
        });

        // 返回按钮事件
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭当前窗口
                // 这里可以添加代码返回到 MainUI 或其他界面
            }
        });

        setVisible(true);
    }

    private void loadStudentData(List<Student> students) {
        tableModel.setRowCount(0); // 清空表格
        for (Student student : students) {
            tableModel.addRow(new Object[]{student.getId(), student.getName()});
        }
    }

    private void searchStudents() {
        String searchText = searchField.getText().trim();
        List<Student> students;
        if (!searchText.isEmpty()) {
            students = studentService.getStudentListByName(searchText, currentClass);
        } else {
            students = studentService.getAllStudents(currentClass);
        }
        loadStudentData(students);
    }

    private void deleteSelectedStudent() {
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            int studentId = (int) tableModel.getValueAt(selectedRow, 0);
            studentService.deleteStudent(studentId);
            tableModel.removeRow(selectedRow);
            JOptionPane.showMessageDialog(this, "学生删除成功");
        } else {
            JOptionPane.showMessageDialog(this, "请先选择一个学生", "警告", JOptionPane.WARNING_MESSAGE);
        }
    }
}
